﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web;
using System.Collections;
using System.Web.Mvc;
using System.Web.Routing;

namespace AnyGrid.Tests
{
    [TestClass]
    public class AnyGridHtmlHelpersTests
    {
        [TestMethod]
        public void Grid_should_render_HTML_table()
        {
            var vc = new ViewContext();
            vc.HttpContext = new FakeHttpContext();
            vc.RouteData = new RouteData();
            vc.RouteData.Values.Add("action", "list");
            var hh = new HtmlHelper(vc, new FakeViewDataContainer());
            var model = new GridViewModel<TestViewModel>();
            var options = new GridViewOptions { GridHtmlId = "test" };

            var result = AnyGridHtmlHelpers.Grid(hh, model, options, null).ToString();

            Assert.AreEqual("<table class=\"anyGrid\" id=\"test\"></table>", result);
        }

        [TestMethod]
        public void Grid_should_render_HTML_div_element_for_pager()
        {
            var vc = new ViewContext();
            vc.RouteData = new RouteData();
            vc.RouteData.Values.Add("action", "list");
            vc.HttpContext = new FakeHttpContext();
            var hh = new HtmlHelper(vc, new FakeViewDataContainer());
            var model = new GridViewModel<TestViewModel>();
            var options = new GridViewOptions { GridHtmlId = "test", Pager = true, PagerHtmlId = "pager" };

            var result = AnyGridHtmlHelpers.Grid(hh, model, options, null).ToString();

            Assert.AreEqual("<table class=\"anyGrid\" id=\"test\"></table><div id=\"pager\"></div>", result);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Grid_should_throw_when_helper_argument_null()
        {
            var model = new GridViewModel<object>();

            AnyGridHtmlHelpers.Grid(null, model, null, null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Grid_should_throw_when_model_argument_null()
        {
            var hh = new HtmlHelper(null, null);

            AnyGridHtmlHelpers.Grid(hh, null, null, null);
        }

        [TestMethod]
        public void GridScriptTag_should_return_a_script_when_there_is_a_grid()
        {
            var vc = new ViewContext();
            vc.RouteData = new RouteData();
            vc.RouteData.Values.Add("action", "list");
            vc.HttpContext = new FakeHttpContext();
            var hh = new HtmlHelper(vc, new FakeViewDataContainer());

            var grid = AnyGridHtmlHelpers.Grid(hh, new GridViewModel<TestViewModel>(), null, null);
            var result = AnyGridHtmlHelpers.GridScriptTag(hh).ToString();

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Contains("<script"));
        }

        [TestMethod]
        public void GridScriptTag_should_return_empty_string_when_there_are_no_grids()
        {
            var vc = new ViewContext();
            vc.HttpContext = new FakeHttpContext();
            var hh = new HtmlHelper(vc, new FakeViewDataContainer());

            var result = AnyGridHtmlHelpers.GridScriptTag(hh).ToString();

            Assert.AreEqual(string.Empty, result);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GridScriptTag_should_throw_when_helper_argument_null()
        {
            AnyGridHtmlHelpers.GridScriptTag(null);
        }

        private class FakeHttpContext : HttpContextBase
        {
            private Dictionary<object, object> _items = new Dictionary<object, object>();
            public override IDictionary Items { get { return _items; } }
        }

        private class FakeViewDataContainer : IViewDataContainer
        {
            private ViewDataDictionary _viewData = new ViewDataDictionary();
            public ViewDataDictionary ViewData { get { return _viewData; } set { _viewData = value; } }
        }

        private class TestViewModel
        {
            public int Id { get; set; }
            public string Description { get; set; }
        }
    }
}
